package com.slashvision.im.tool

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by Meiji on 2016/12/13.
 */
object TimeUtil {

    fun toTitleDate(timestamp: Long) = SimpleDateFormat("yyyy年MM月dd日\n${TSTR_7(timestamp)}HH:mm").format(Date(timestamp))
    fun dates(timestamp: Long): List<Int> = TSTR_3(timestamp).split("-").run {
        val mList = mutableListOf<Int>()
        this.forEach {
            mList.add(it.toInt())
        }
        mList
    }
    fun isOneYear(year: Int) = TSTR_8(System.currentTimeMillis()).equals(year.toString())
    fun isOneYear(timestamp: Long) = TSTR_8(timestamp).equals(TSTR_8(System.currentTimeMillis()))



    /**
     * 获取当前的时间，字符串格式为"yyyyMMddHHmmss"
     * @return 字符串格式的时间
     */
    fun NOW(): String {
        return try {
            val pattern = "yyyyMMddHHmmss"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(Date())
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yyyyMMddHHmmss"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR(time: Long): String {
        return try {
            val pattern = "yyyyMMddHHmmss"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(time)
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yyyyMMddHHmmss"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_2(time: Long): String {
        return try {
            val pattern = "yyyy/MM/dd HH:mm"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(time)
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yyyy-MM-dd"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_3(time: Long): String {
        return try {
            val pattern = "yyyy-MM-dd"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(time)
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yyyy年MM月dd日"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_4(time: Long): String {
        return try {
            val sdf = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
            sdf.format(Date(time))
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yyyy年MM月"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_5(time: Long): String {
        return try {
            val pattern = "yyyy年MM月"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(time)
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"HH:mm"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_6(time: Long): String {
        return try {
            val pattern = "HH:mm"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            formater.format(time)
        } catch (e: Exception) {
            ""
        }
    }

    fun TSTR_7(time: Long): String {
        return try {
            val pattern = "HH"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            val str = formater.format(time)
            when (str.toInt()) {
                in 0..6 -> "凌晨"
                in 7..12 -> "上午"
                in 13..13 -> "中午"
                in 14..18 -> "下午"
                in 19..24 -> "晚上"
                else -> ""
            }
        } catch (e: Exception) {
            ""
        }
    }

    /**
     * 格式化指定时间，字符串格式为"yy"
     * @param time 指定时间，长整型
     * @return 字符串格式的时间
     */
    fun TSTR_8(time: Long): String {
        val pattern = "yyyy"
        val formater = SimpleDateFormat(pattern, Locale.getDefault())
        return formater.format(time)
    }

    fun TSTR_9(time: Long): String {
        return try {
            val pattern = "MM月dd日 HH:mm"
            val formater = SimpleDateFormat(pattern, Locale.getDefault())
            val str = formater.format(time)
            when (str.toInt()) {
                in 0..6 -> "凌晨"
                in 7..12 -> "上午"
                in 13..13 -> "中午"
                in 14..18 -> "下午"
                in 19..24 -> "晚上"
                else -> ""
            }
        } catch (e: Exception) {
            ""
        }
    }


    /**
     * String 转 Data
     */
    fun stringConvertDate(time: String?): Date? {
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
        var data: Date? = null
        try {
            data = sdf.parse(time)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return data
    }

    /**
     * 返回发布时间距离当前的时间
     */
    fun timeAgo(createdTime: Date?): String {
        val format = SimpleDateFormat("MM-dd HH:mm", Locale.CHINA)
        return if (createdTime != null) {
            val agoTimeInMin =
                (Date(System.currentTimeMillis()).time - createdTime.time) / 1000 / 60
            // 如果在当前时间以前一分钟内
            when {
                agoTimeInMin <= 1 -> {
                    "刚刚"
                }
                agoTimeInMin <= 60 -> {
                    // 如果传入的参数时间在当前时间以前10分钟之内
                    agoTimeInMin.toString() + "分钟前"
                }
                agoTimeInMin <= 60 * 24 -> {
                    (agoTimeInMin / 60).toString() + "小时前"
                }
                agoTimeInMin <= 60 * 24 * 2 -> {
                    (agoTimeInMin / (60 * 24)).toString() + "天前"
                }
                else -> {
                    format.format(createdTime)
                }
            }
        } else {
            format.format(Date(0))
        }
    }

    /**
     * 根据时间戳 返回发布时间距离当前的时间
     */
    fun getTimeStampAgo(timeStamp: String): String {
        val time = timeStamp.toLong()
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        val string = sdf.format(time * 1000L)
        var date: Date? = null
        try {
            date = sdf.parse(string)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return timeAgo(date)
    }

    val currentTimeStamp: String
        get() = (System.currentTimeMillis() / 1000).toString()

    /**
     * 是否为同一天
     */
    fun isOneDay(fTime: Long, sTime: Long):Boolean {
        val todayCal=Calendar.getInstance()
        val dateCal =Calendar.getInstance()
        todayCal.time = Date(fTime)
        dateCal.time = Date(sTime)
        return if(todayCal != null && dateCal != null) {
            todayCal.get(Calendar.YEAR) == dateCal.get(Calendar.YEAR)
                    && todayCal.get(Calendar.ERA) == dateCal.get(Calendar.ERA)
                    && todayCal.get(Calendar.DAY_OF_YEAR) == dateCal.get(Calendar.DAY_OF_YEAR)
        } else {
            false
        }
    }

    fun isOneDay(year: Int, month: Int, day: Int) = TSTR_3(System.currentTimeMillis()).equals("${year}-${month}-${day}")

    /**
     * 是否为一周内的数据
     */
    fun isOneWeak(ftime: Long, sTime: Long):Boolean {
        val todayCal=Calendar.getInstance()
        val dateCal =Calendar.getInstance()
        todayCal.time = Date(ftime)
        dateCal.time = Date(sTime)
        var subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR)
        return if (subYear == 0) {
            todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR)
        } else {
            false
        }
    }

    /**
     * 是否为一周内的数据
     */
    fun isOneWeak(time: Long):Boolean {
        val todayCal=Calendar.getInstance()
        val dateCal =Calendar.getInstance()
        todayCal.time = Date()
        dateCal.time = Date(time)
        var subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR)
        return if (subYear == 0) {
            todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR)
        } else {
            false
        }
    }

    /**
     * 是否为同一个月
     */
    fun isOneMonth(time: Long):Boolean {
        val todayCal=Calendar.getInstance()
        val dateCal =Calendar.getInstance()
        todayCal.time = Date()
        dateCal.time = Date(time)
        var subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR)
        return if (subYear == 0) {
            todayCal.get(Calendar.MONTH) == dateCal.get(Calendar.MONTH)
        } else {
            false
        }
    }

    /**
     * 是否为同一个月
     */
    fun isOneMonth(time1: Long, time2:Long):Boolean {
        val todayCal=Calendar.getInstance()
        val dateCal =Calendar.getInstance()
        todayCal.time = Date(time1)
        dateCal.time = Date(time2)
        var subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR)
        return if (subYear == 0) {
            todayCal.get(Calendar.MONTH) == dateCal.get(Calendar.MONTH)
        } else {
            false
        }
    }

}
